package ledrpt

import org.kdx.DateUtils

import java.text.SimpleDateFormat

class BoetvController {
    def dynamicQueryService
    def worptlist() {
        params.SYS_QUERY_NAME='worptList'
        def worptList =dynamicQueryService.queryForList(params)
        [worptList:worptList,woId:params.woId]
    }
    def exportExcel() {
        def sysQueryName
        def tab = params.timeType// ?:'H'
        //��?
        if(tab.equals('H')) {
            sysQueryName = params.SYS_QUERY_NAME+'HList'
        }else if (tab.equals('D')){
            sysQueryName = params.SYS_QUERY_NAME+'DList'
        }else if (tab.equals('W')){
            sysQueryName = params.SYS_QUERY_NAME+'WList'
        }else if (tab.equals('M')){
            sysQueryName = params.SYS_QUERY_NAME+'MList'
        }else if (tab.equals('Q')){
            sysQueryName = params.SYS_QUERY_NAME+'QList'
        }else if (tab.equals('Y')){
            sysQueryName = params.SYS_QUERY_NAME+'YList'
        }else{
            sysQueryName = params.SYS_QUERY_NAME
        }
        params.startNo = 1
        params.endNo = 99999999999999999
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(dataList.size()>0){
            tableHeadList =  dataList[0].keySet()
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList])
    }
    def exportExce2() {
        def sysQueryName
        def tab = params.timeType// ?:'H'
        //��?
        if(tab.equals('H')) {
            sysQueryName = params.SYS_QUERY_NAME+'HList'
        }else if (tab.equals('D')){
            sysQueryName = params.SYS_QUERY_NAME+'DList'
        }else if (tab.equals('W')){
            sysQueryName = params.SYS_QUERY_NAME+'WList'
        }else if (tab.equals('M')){
            sysQueryName = params.SYS_QUERY_NAME+'MList'
        }else if (tab.equals('Q')){
            sysQueryName = params.SYS_QUERY_NAME+'QList'
        }else if (tab.equals('Y')){
            sysQueryName = params.SYS_QUERY_NAME+'YList'
        }else{
            sysQueryName = params.SYS_QUERY_NAME
        }
        params.startNo = 1
        params.endNo = 99999999999999999
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(dataList.size()>0){
            tableHeadList =  dataList[0].keySet()
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList])
    }
    //�޸�finaly��
    def updateworpt(DwsWoRpt c){

        render (view:'updateworpt',
                model:[worpt:c])


    }
    //�����޸�finaly�����Ϣ
    def addworpt(DwsWoRpt cfu){

        cfu.save()
        render """{
                    "statusCode":"200",
                    "message":"${message(code: 'default.created.message', args: [message(code: 'DwsWoRpt.label', default: 'DwsWoRpt'), cfu.id])}",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
    }
    //ɾ��defect����Ϣ
    def deleteworpt(DwsWoRpt c){

        c.delete()
        render """{
                    "statusCode":"200",
                    "message":"${message(code: 'default.created.message', args: [message(code: 'DwsWoRpt.label', default: 'DwsWoRpt'), c.id])}",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""

    }

    def yieldChart() {
        params.SYS_QUERY_NAME = 'productList'
        def productList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'lineList'
        def lineList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'timeType'
        def timeList = dynamicQueryService.queryForList(params)
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
        def dayList = DateUtils.getDayList(startTime, endTime)
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        params.timetype=params.timetype?:'H'
        params.SYS_QUERY_NAME = 'yieldChartHList'
        def dataListNo = dynamicQueryService.queryForList(params)
        //params.SYS_QUERY_NAME = 'yieldChartHCount'
        def totalCount = dataListNo.size()
        params.SYS_QUERY_NAME = 'yieldChartH'
        def dataList = dynamicQueryService.queryForList(params)
        println dataList
        def legendDataList = ['QTY','YIELD',]
        def percentItemList = ['YIELD']
        def seriesData = []
        for(def k=0;k<legendDataList.size();k++){
            def seryHourData = []
            for(def j=0;j<dayList.size();j++){
                def flag = false;
                for(def i=0;i<dataList.size();i++){
                    if(dayList[j]==dataList[i].V_Date){
                        seryHourData.push(dataList[i][legendDataList[k]])
                        flag = true;
                    }
                }
                if(!flag){
                    seryHourData.push(0)
                }
            }
            seriesData.push(seryHourData)
        }
        println seriesData
        def testDataList = []
        [productList:productList,lineList:lineList,timeList:timeList,startTime:startTime,endTime:endTime,productid:params.productid?:'',percentItemList:percentItemList,
         lineid:params.lineid?:'',timetype:params.timetype?:'H',dayList:dayList,legendDataList:legendDataList,seriesData:seriesData,testDataList:testDataList,
         pageSize:pageSize,totalCount:totalCount,pageSizeList:pageSizeList,dataListNo:dataListNo]
    }
    def yieldlist() {
        params.SYS_QUERY_NAME='yieldlist'
        def yieldList =dynamicQueryService.queryForList(params)
        [yieldList:yieldList]
    }
    //�޸�yield��
    def updateyield(DwsOutyieldHdwm c){

        render (view:'updateyield',
                model:[yield:c])


    }
    //�����޸�yield�����Ϣ
    def addyield(DwsOutyieldHdwm cfu){

        cfu.save()
        render """{
                    "statusCode":"200",
                    "message":"${message(code: 'default.created.message', args: [message(code: 'DwsOutyieldHdwm.label', default: 'DwsOutyieldHdwm'), cfu.id])}",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
    }
    //ɾ��yield��ı���Ϣ
    def deleteyield(DwsOutyieldHdwm c){

        c.delete()
        render """{
                    "statusCode":"200",
                    "message":"${message(code: 'default.created.message', args: [message(code: 'DwsOutyieldHdwm.label', default: 'DwsOutyieldHdwm'), c.id])}",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""

    }
}
